clear

*********************** Bloomberg data **************************************
* Panel bloomberg data
use "./data/3monthLP_panel"

* Drop Venezuela, US, Brazil, Mexico, Taiwan, Greece
keep if ccode < 49 
drop if ccode == 5 | ccode == 28 | ccode == 45 | ccode == 15 


drop mps mpu
rename mps_2day mps
rename mpu_2day mpu


* Crisis dummy
gen crisis = 0
replace crisis = 1 if date >= td(01dec2007) & date < td(01jul2009)

***** variables to store the impulse response (vector of betas from the LP regressions) and standard errors
gen adv_3m_mps=.
gen adv_std_3m_mps=.

gen adv_3m_mpu=.
gen adv_std_3m_mpu=.

gen eme_3m_mps=.
gen eme_std_3m_mps=.

gen eme_3m_mpu=.
gen eme_std_3m_mpu=.

* number of lags
local MaxLPLags 0

* horizon
local horizon 548

*************************** LP regressions *************************************
* Reset tsset
egen time2 = group(date)
sort ccode time2
tsset ccode time2

forvalues i=0/`horizon' {

	* 3-month
	xtscc three_m`i' mps mpu if advanced == 1, fe lag(3) 
	eststo ols_3m`i'
	
	replace adv_3m_mps  = _b[mps] if _n == `i'+1
	replace adv_std_3m_mps = _se[mps] if _n == `i'+1	
	
	replace adv_3m_mpu  = _b[mpu] if _n == `i'+1
	replace adv_std_3m_mpu = _se[mpu] if _n == `i'+1	
	
	eststo clear
	
}




forvalues i=0/`horizon' {

	xtscc three_m`i' mps mpu if advanced == 0, fe lag(3) 
	eststo ols_3m`i'
	
	replace eme_3m_mps  = _b[mps] if _n == `i'+1
	replace eme_std_3m_mps = _se[mps] if _n == `i'+1		
	
	replace eme_3m_mpu  = _b[mpu] if _n == `i'+1
	replace eme_std_3m_mpu = _se[mpu] if _n == `i'+1			
	
	eststo clear
}


****************************************************************
*** Baseline OLS LP graphs
****************************************************************

gen Days = _n-1 if _n <= `horizon' +1
replace Days = . if Days == 0

* zero line
gen zero = 0 if _n <= `horizon' +1		

***** create confidence bands (in this case 68 and 95%) ****
	scalar sig1 = 0.05	 
	scalar sig2 = 0.32	 
	
 
	gen up_3m_adv = adv_3m_mpu + invnormal(1-sig1/2)*adv_std_3m_mpu if _n <= (`horizon' + 1)
	gen dn_3m_adv = adv_3m_mpu - invnormal(1-sig1/2)*adv_std_3m_mpu if _n <= (`horizon' + 1)

	gen up2_3m_adv = adv_3m_mpu + invnormal(1-sig2/2)*adv_std_3m_mpu if _n <= (`horizon' + 1)
	gen dn2_3m_adv = adv_3m_mpu - invnormal(1-sig2/2)*adv_std_3m_mpu if _n <= (`horizon' + 1)
	
	gen up_3m_eme = eme_3m_mpu + invnormal(1-sig1/2)*eme_std_3m_mpu if _n <= (`horizon' + 1)
	gen dn_3m_eme = eme_3m_mpu - invnormal(1-sig1/2)*eme_std_3m_mpu if _n <= (`horizon' + 1)

	gen up2_3m_eme = eme_3m_mpu + invnormal(1-sig2/2)*eme_std_3m_mpu if _n <= (`horizon' + 1)
	gen dn2_3m_eme = eme_3m_mpu - invnormal(1-sig2/2)*eme_std_3m_mpu if _n <= (`horizon' + 1)
	
	
	
	gen up_3m_adv_mps = adv_3m_mps + invnormal(1-sig1/2)*adv_std_3m_mps if _n <= (`horizon' + 1)
	gen dn_3m_adv_mps = adv_3m_mps - invnormal(1-sig1/2)*adv_std_3m_mps if _n <= (`horizon' + 1)

	gen up2_3m_adv_mps = adv_3m_mps + invnormal(1-sig2/2)*adv_std_3m_mps if _n <= (`horizon' + 1)
	gen dn2_3m_adv_mps = adv_3m_mps - invnormal(1-sig2/2)*adv_std_3m_mps if _n <= (`horizon' + 1)
	
	gen up_3m_eme_mps = eme_3m_mps + invnormal(1-sig1/2)*eme_std_3m_mps if _n <= (`horizon' + 1)
	gen dn_3m_eme_mps = eme_3m_mps - invnormal(1-sig1/2)*eme_std_3m_mps if _n <= (`horizon' + 1)

	gen up2_3m_eme_mps = eme_3m_mps + invnormal(1-sig2/2)*eme_std_3m_mps if _n <= (`horizon' + 1)
	gen dn2_3m_eme_mps = eme_3m_mps - invnormal(1-sig2/2)*eme_std_3m_mps if _n <= (`horizon' + 1)



************************ IRF Graphs ********************************************

*** MPU ***

local three_m_adv "Advanced"
local three_m_eme "Emerging"

* 3 month
grstyle init
grstyle set plain, horizontal grid dotted noextend

	twoway (rarea up2_3m_adv dn2_3m_adv  Days, ///
	fcolor(gs12) lcolor(white) lpattern(solid)) ///
	(line adv_3m_mpu Days, lcolor(blue) ///
	lpattern(solid) lwidth(medium)) ///
	(line zero Days, lcolor(black)), ///
	xtitle("Months", size(vsmall)) ///
	xscale(range(1 548)) ///
	xlabel(0 "0" 90 "3" 180 "6" 270 "9" 360 "12" 450 "15" 540 "18") ///
	yscale(range(-0.2 0.4)) ///
	ylabel(-0.2 "-.2" -0.1 "-.1" 0 "0" 0.1 ".1" 0.2 ".2" 0.3 ".3" 0.4 ".4") ///
	title("`three_m_adv'", color(black) size(small)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(adv, replace) 
	graph save   three_m_adv.gph, replace
	
	
grstyle init
grstyle set plain, horizontal grid dotted noextend
	
	twoway (rarea up2_3m_eme dn2_3m_eme  Days, ///
	fcolor(gs12) lcolor(white) lpattern(solid)) ///
	(line eme_3m_mpu Days, lcolor(red) ///
	lpattern(solid) lwidth(medium)) ///
	(line zero Days, lcolor(black)), ///
	xtitle("Months", size(vsmall)) ///
	xscale(range(1 548)) ///
	xlabel(0 "0" 90 "3" 180 "6" 270 "9" 360 "12" 450 "15" 540 "18") ///
	yscale(range(-0.2 0.4)) ///
	ylabel(-0.2 "-.2" -0.1 "-.1" 0 "0" 0.1 ".1" 0.2 ".2" 0.3 ".3" 0.4 ".4") ///
	title("`three_m_eme'", color(black) size(small)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	legend(off) name(eme, replace) 
	graph save   three_m_eme.gph, replace


	graph combine three_m_adv.gph three_m_eme.gph, col(2) ycommon name(threem, replace) graphregion(color(white)) plotregion(color(white))
	graph save 3m_LP.gph, replace

	
********************************************************************************